package thread.forkjoinpool;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;


public class ForkJoinTest {

    public static void main(String[] args) throws Exception {
        // 最多5个线程同时执行任务
        ForkJoinPool pool = new ForkJoinPool(5);


        // 有返回结果的任务
        MyRecursiveTask task = new MyRecursiveTask(0, 500);
        Future<Integer> sum = pool.submit(task);
        System.out.println("result: " + sum.get());


        // 无返回结果的任务
        MyRecursiveAction action = new MyRecursiveAction(0, 500);
        pool.submit(action);


        // 2秒后所有线程进入阻塞状态，不管完没完成任务
        pool.awaitTermination(2, TimeUnit.SECONDS);
    }
}
